package com.hkyl.hospital.service.impl;



import com.github.pagehelper.PageHelper;
import com.hkyl.hospital.entity.Dept;
import com.hkyl.hospital.entity.DeptExample;
import com.hkyl.hospital.mapper.DeptMapper;
import com.hkyl.hospital.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sjh
 * @date 2023/07/08 17:16:55
 */
@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Autowired
    private CustomDeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Dept> findAllByMedical() {
        DeptExample deptExample = new DeptExample();
        deptExample.or().andDeptNameEqualTo("医务科");
        Dept dept = deptMapper.selectByExample(deptExample).get(0);
        DeptExample example = new DeptExample();
        example.or().andParentIdEqualTo(dept.getDeptId());
        List<Dept> depts = deptMapper.selectByExample(example);
        for (Dept dept1 : depts) {
            getChildren(deptMapper.selectByExample(null),dept1);
        }
        setDoctors(depts);
        return depts;
    }
    @Override
    public int insertDept(Dept dept) {
        return deptMapper.insertSelective(dept);
    }

    @Override
    public int deleteDept(Integer deptId) {
        return deptMapper.deleteByPrimaryKey(deptId);
    }

    @Override
    public int updateDept(Dept dept) {
        return deptMapper.updateByPrimaryKeySelective(dept);
    }

    @Override
    public List<Dept> selectDept(Integer pageNum, Integer pageSize) {
        List<Dept> depts = deptMapper.selectByExample(null);
        PageHelper.startPage(pageNum, pageSize);
        DeptExample deptExample = new DeptExample();
        deptExample.or().andParentIdIsNull();
        List<Dept> root = deptMapper.selectByExample(deptExample);
        for (Dept dept : root) {
            getChildren(depts, dept);
        }
        return root;
    }

    @Override
    public List<Dept> selectDeptAll() {
        List<Dept> depts = deptMapper.selectByExample(null);
        DeptExample deptExample = new DeptExample();
        deptExample.or().andParentIdIsNull();
        List<Dept> root = deptMapper.selectByExample(deptExample);
        for (Dept dept : root) {
            getChildren(depts, dept);
        }
        return root;
    }

    @Override
    public List<Dept> findAllWithDoctorByMedical() {
        List<Dept> depts = findAllByMedical();
        setDoctors(depts);
        return depts;
    }

    private void setDoctors(List<Dept> depts) {
        List<User> users = userMapper.selectByExample(null);
        for (Dept dept : depts) {
            if (dept.getChildren() != null) {
                setDoctors(dept.getChildren());
            }
            List<User> userList = users.stream().filter(user -> user.getDeptId() == dept.getDeptId()).collect(Collectors.toList());
            dept.setDoctors(userList);
        }

    }
    private static void getChildren(List<Dept> depts, Dept parent) {
        for (Dept childrenDept : depts) {
            if (parent.getDeptId() == childrenDept.getParentId()) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(childrenDept);
                getChildren(depts, childrenDept);
            }
        }
    }
}
